﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Windows;

namespace Com.Lancetinc.Evs.Helpers;

public static class WinSystemHelper
{
    public static Dictionary<string, SerialPortInfo> GetSerialPortInfo()
    {
        using var searcher =
            new ManagementObjectSearcher(
                "SELECT * FROM Win32_PnPEntity WHERE ClassGuid = '{4d36e978-e325-11ce-bfc1-08002be10318}'");

        var dic = new Dictionary<string, SerialPortInfo>();
        // guid={4d36e978-e325-11ce-bfc1-08002be10318}
        // name=Silicon Labs CP210x USB to UART Bridge (COM4)
        // caption=Silicon Labs CP210x USB to UART Bridge (COM4)
        foreach (var queryObj in searcher.Get().OfType<ManagementObject>())
        {
            var name = queryObj["Name"].ToString();
            if (!name.EndsWith(")"))
                continue;

            var idx = name.LastIndexOf("(");
            if (idx == -1)
                continue;

            var portName = name.Substring(idx + 1, name.Length - idx - 2);
            var info = new SerialPortInfo
            {
                PortName = portName,
                DeviceName = name,
                Caption = queryObj["Caption"]?.ToString(),
                Description = queryObj["Description"]?.ToString(),
                Manufacturer = queryObj["Manufacturer"]?.ToString(),
            };
            dic[portName] = info;
        }

        return dic;
    }

    private static void GetSerialPort()
    {
        try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_PnPEntity");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                var name = queryObj["Name"];
                var cap = queryObj["Caption"];
                var guid = queryObj["ClassGuid"];
                string pnpDeviceID = queryObj["PNPDeviceID"].ToString();
                if ("{4d36e978-e325-11ce-bfc1-08002be10318}".Equals(guid))
                {
                    var device = queryObj;
                    Console.WriteLine("Caption: " + device["Caption"]);
                    Console.WriteLine("Description: " + device["Description"]);
                    Console.WriteLine("Device ID: " + device["DeviceID"]);
                    Console.WriteLine("Class GUID: " + device["ClassGuid"]);
                    Console.WriteLine("Manufacturer: " + device["Manufacturer"]);
                    Console.WriteLine("PNP Device ID: " + device["PNPDeviceID"]);
                    Console.WriteLine("Status: " + device["Status"]);
                    Console.WriteLine("System Name: " + device["SystemName"]);
                }
                Console.WriteLine($"guid={guid},name={name},cap={cap}");
                //if (queryObj["Caption"].ToString().Contains("(COM"))
                //{
                //    Console.WriteLine("serial port : {0}", queryObj["Caption"]);
                //}

            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show(e.Message);
        }

    }
}

public class SerialPortInfo
{
    public string PortName { get; set; }

    public string DeviceName { get; set; }

    public string Caption { get; set; }

    public string Description { get; set; }

    public string Manufacturer { get; set; }
}